// pages/goods-list/index.js
import {
    getNavigationbar,
    ReMallShopping
} from "../../api/index.js"
import {
    ReDistributionMall
} from "../../api/shipping.js"
Page({
    options: {
        styleIsolation: "shared"
    },
    /**
     * 页面的初始数据
     */
    data: {
        nvaigateName: "商品列表",
        nvaigateState: false,
        isAshow:false,
        isAhow: false,isAshows:false,
        orderListMian: false,
        kFlag: false,
        pecs: false,
        lastScrollTop: 0,
        scrollViewHeight: 0,
        height: 0,
        goodsList: [],
        lineState: false,
        isSpecs: false,
        isSpecsshipping: false,
        columns: [],
        history: [],
        subdata: {},
        lowPrice: 0,
        highPrice: 0,
        scrollTop: 0,
        figi: false,
        // 价格
        priceData: {
            lowPrice: '',
            highPrice: ''
        },
        isinternal: '', //是否进口
        keyword: '',
        obj: {},
        loading: false,
        showOUt:true,
        currentPage: 1, // 当前页码
        totalPages: 10, // 每页数据条数
        numGood: 0,
        imgs: '',
        isScrolling: false,
        showMians: false,
        nvaigateState: false,
        item: {},
        brandid: '',
        orderFlag: false,
        navigateToUrl: ''
    },
    handleEditCart() {
        wx.navigateBack()
    },
    indecocki() {

    },
    onScrollViewScroll: function (e) {
        if (e.detail.scrollTop >= 600) { // 当滚动到200px位置时添加样式
            this.setData({
                isAshows: true,
                // scrollViewHeight:700
            });
        } else {
            this.setData({
                isAshows: false
            });
        }
        console.log('eeee',e.detail.scrollTop)
        // 在这里可以处理scroll-view滚动时的逻辑
    },
    /**
     * 蒙版
     * @param {*} e 
     */
    handlemaskClick(e) {
        if(e.detail == false) {
            this.setData({
                isAshow: false
            })
            if (this.data.subdata.shipping == true) {
                this.setData({
                    nvaigateState: true,
                })
            } else {
                this.setData({
                    nvaigateState: false,
                })
            }
            return
        }
        this.setData({
            figi: true
        })
        this.data.history = this.data.history
        console.log('e.detail11', e.detail, this.data.history)
        if (this.data.history.length == 0) {
            this.data.history.push({
                type: 1,
                data: []
            }, {
                type: 2,
                data: []
            }, {
                type: 3,
                data: []
            }, {
                type: 4,
                data: []
            });
        }

        e.detail.forEach((item, index) => {
            if (index == 0) {
                this.data.brandList.map(element => element.checked = false)
                this.data.brandList.forEach(element => {
                    item.selectInfo.forEach(info => {
                        if (info.brandid == element.brandid) {
                            element.checked = true;
                        }
                    })
                })
                this.data.history.forEach(element => {
                    if (element.type == 3) {
                        element.data = item.selectInfo
                    }
                })
            } else if (index == 1) {
                this.data.catList.map(element => element.checked = false)
                this.data.catList.forEach(element => {
                    item.selectInfo.forEach(info => {
                        if (info.id == element.id) {
                            element.checked = true;
                        }
                    })
                })
                this.data.history.forEach(element => {
                    if (element.type == 2) {
                        element.data = item.selectInfo
                    }
                })
            } else if (index == 2) {
                this.data.petTypeList.map(element => element.checked = false)
                this.data.petTypeList.forEach(element => {
                    item.selectInfo.forEach(info => {
                        if (info.id == element.id) {
                            element.checked = true;
                        }
                    })
                })
                this.data.history.forEach(element => {
                    if (element.type == 1) {
                        element.data = item.selectInfo
                    }
                })
            } else if (index == 3) {
                this.data.history.forEach(element => {
                    if (element.type == 4) {
                        element.data = this.data.isinternal == 2 ? [{
                            id: 1,
                            name: '进口商品'
                        }] : item.selectInfo
                    }
                })
            }
        })
        wx.nextTick(() => {
            this.setData({
                history: this.data.history,
                figi: true
            }, () => {
                this.computeScrollViewHeight()
            })
        })
        // 使用map方法取出id并组成字符串
        let pettype = this.data.history[0].data.map(item => {
            if (item.numId) {
                return item.numId;
            } else if (item.id) {
                return item.id;
            }
        }).join(',');

        let scatid = this.data.history[1].data.map(item => item.id).join(',');
        let brandid = this.data.history[2].data.map(item => item.brandid).join(',');
        let activityregion = ''
        console.log('this.data.history', this.data.history)
        if (this.data.history.length != 0) {
            activityregion = this.data.history[3].data.map(item => item.id);
        }
        console.log('this.data.history[3].data.map(item => item.id)', activityregion)
        let combinedString = ''
        if (this.data.lowPrice != '' && this.data.highPrice != '') {
            combinedString = this.data.lowPrice + ',' + this.data.highPrice;
        }
        console.log('pettype', pettype, scatid, brandid, combinedString); // 输出结果为 "96,4,9"
        this.setData({
            brandid,
            figi: true,
            subdata: {
                ...this.data.subdata,
                pettype,
                scatid,
                brandid
            },
            currentPage: 1
        })
        let data = {
            pettype: pettype,
            bcatids: '',
            scatid: scatid,
            brandid: brandid,
            price_block: combinedString,
            keyword: this.data.keyword,
            minprice: this.data.lowPrice,
            maxprice: this.data.highPrice,
            activity_region: activityregion != '' ? parseInt(activityregion.join('')) : '',
        }

        if (this.data.subdata.shipping == true) {
            this.getReDistributionMall(data);
        } else {
            this.getProductlist(data);
        }
        console.log("你点击了取消", e, this.data.history);
        const app = getApp();
        app.globalData.historyList = {
            ...this.data.subdata,
            ...data,
            historyList: [...this.data.history]
        };
        this.data.goodsList = []

        if (this.data.subdata.shipping == true) {
            this.setData({
                nvaigateState: true,
            })
        } else {
            this.setData({
                nvaigateState: false,
            })
        }
    },
    /**
     * 选择筛选类目回调函数方法
     * @param {*} e 
     */
    // 数据处理函数，根据index和接口返回的数据处理columns和height
    // 处理选项切换事件
    handleOnTabs(e) {
        console.log("this.data.history", this.data, this.data.history)

        const index = e.detail;
        const {
            brandList,
            catList,
            petTypeList
        } = this.data;
        let columns = [];
        let height = 0;
        switch (index) {
            case 0:
                columns = brandList.map(element => ({
                    ...element,
                    type: 3
                }));
                height = 360;
                break;
            case 1:
                columns = catList.map(element => ({
                    ...element,
                    type: 2
                }));
                height = 170;
                break;
            case 2:
                columns = petTypeList.map((element, index) => {
                    let numId;
                    switch (element.name) {
                        // 处理numId逻辑
                    }
                    return {
                        ...element,
                        type: 1,
                        //    index,
                        numId: numId
                    };
                });
                height = 170;
                break;
            case 3:
                height = 170;
                break;
        }
        this.setData({
            nvaigateState: false,
            columns,
            height,
            history: this.data.history
        });
    },
    /**
     * 确认筛选内容回调函数方法
     * @param {*} e 
     */
    handleOnconfirm(e) {
        this.setData({
            figi: true
        })
        this.data.history = this.data.history
        console.log('e.detail11', e.detail, this.data.history)
        if (this.data.history.length == 0) {
            this.data.history.push({
                type: 1,
                data: []
            }, {
                type: 2,
                data: []
            }, {
                type: 3,
                data: []
            }, {
                type: 4,
                data: []
            });
        }

        e.detail.forEach((item, index) => {
            if (index == 0) {
                this.data.brandList.map(element => element.checked = false)
                this.data.brandList.forEach(element => {
                    item.selectInfo.forEach(info => {
                        if (info.brandid == element.brandid) {
                            element.checked = true;
                        }
                    })
                })
                this.data.history.forEach(element => {
                    if (element.type == 3) {
                        element.data = item.selectInfo
                    }
                })
            } else if (index == 1) {
                this.data.catList.map(element => element.checked = false)
                this.data.catList.forEach(element => {
                    item.selectInfo.forEach(info => {
                        if (info.id == element.id) {
                            element.checked = true;
                        }
                    })
                })
                this.data.history.forEach(element => {
                    if (element.type == 2) {
                        element.data = item.selectInfo
                    }
                })
            } else if (index == 2) {
                this.data.petTypeList.map(element => element.checked = false)
                this.data.petTypeList.forEach(element => {
                    item.selectInfo.forEach(info => {
                        if (info.id == element.id) {
                            element.checked = true;
                        }
                    })
                })
                this.data.history.forEach(element => {
                    if (element.type == 1) {
                        element.data = item.selectInfo
                    }
                })
            } else if (index == 3) {
                this.data.history.forEach(element => {
                    if (element.type == 4) {
                        element.data = this.data.isinternal == 2 ? [{
                            id: 1,
                            name: '进口商品'
                        }] : item.selectInfo
                    }
                })
            }
        })
        wx.nextTick(() => {
            this.setData({
                history: this.data.history,
                figi: true
            }, () => {
                this.computeScrollViewHeight()
            })
        })
        // 使用map方法取出id并组成字符串
        let pettype = this.data.history[0].data.map(item => {
            if (item.numId) {
                return item.numId;
            } else if (item.id) {
                return item.id;
            }
        }).join(',');

        let scatid = this.data.history[1].data.map(item => item.id).join(',');
        let brandid = this.data.history[2].data.map(item => item.brandid).join(',');
        let activityregion = ''
        console.log('this.data.history', this.data.history)
        if (this.data.history.length != 0) {
            activityregion = this.data.history[3].data.map(item => item.id);
        }
        console.log('this.data.history[3].data.map(item => item.id)', activityregion)
        let combinedString = ''
        if (this.data.lowPrice != '' && this.data.highPrice != '') {
            combinedString = this.data.lowPrice + ',' + this.data.highPrice;
        }
        console.log('pettype', pettype, scatid, brandid, combinedString); // 输出结果为 "96,4,9"
        this.setData({
            brandid,
            figi: true,
            subdata: {
                ...this.data.subdata,
                pettype,
                scatid,
                brandid
            },
            currentPage: 1
        })
        let data = {
            pettype: pettype,
            bcatids: '',
            scatid: scatid,
            brandid: brandid,
            price_block: combinedString,
            keyword: this.data.keyword,
            minprice: this.data.lowPrice,
            maxprice: this.data.highPrice,
            activity_region: activityregion != '' ? parseInt(activityregion.join('')) : '',
        }

        if (this.data.subdata.shipping == true) {
            this.getReDistributionMall(data);
        } else {
            this.getProductlist(data);
        }
        console.log("你点击了确认筛选内容", e, this.data.history);
        const app = getApp();
        app.globalData.historyList = {
            ...this.data.subdata,
            ...data,
            historyList: [...this.data.history]
        };
        this.data.goodsList = []

    },
    getHttpSwiperList() {
        getNavigationbar({
            act: 'banner_list',
            type: '5',
            version: '1',
        }).then(res => {

            this.setData({
                imgs: res[0].child[0].thumb,
                nvaigateState: true,
            })
            console.log('广告', res)
        })
    },
    /**
     * 标签删除
     * @param {*} e 
     */
    handleDelete(e) {
        console.log("eeeeeee", e)
        console.log('e', e.detail.value, this.data.history[2])
        const idToDelete = e.detail.value.type || e.detail.value.classCname || e.detail.value.parentId;
        const indexToDelete = e.detail.value.id || e.detail.value.brandid; // 假设要删除的数据在数组中的索引
          let  indextipToDelete = e.detail.value.tip || e.detail.value.tip;
        this.data.history.forEach(item => {
            item.data
        })
        
        console.log('indexToDelete', indexToDelete)
        // const newHistory = this.data.history.map(element => {
        //     if (element.type == idToDelete) {
        //         let newData
        //         if (element.type == '3') {
        //             newData = element.data.filter((item, index) => item.brandid !== indexToDelete);
        //         } else {
        //             newData = element.data.filter((item, index) => item.id !== indexToDelete);
        //         }
        //         return {
        //             type: element.type,
        //             data: newData
        //         };
        //     } else {
        //         return element;
        //     }
        // });
        if (indextipToDelete == true) {
            let newData;
            this.data.history = this.data.history.map(element => {
                if (element.type == 4) {
                    newData = element.data.filter((item, index) => item.id !== indexToDelete);
                    console.log('newData', newData);
                    return {
                        type: element.type,
                        data: newData
                    };
                }
                return element; // 返回原始元素，除非类型为4
            });
         
        } else {
            // 假设要删除的数据在数组中的索引
            this.data.history = this.data.history.map(element => {
               
                if (element.type == idToDelete) {
                    let newData
                    if (element.type == 3) {
                        newData = element.data.filter((item, index) => item.brandid !== indexToDelete);

                    } else {
                        newData = element.data.filter((item, index) => item.id !== indexToDelete);
                    }
                    return {
                        type: element.type,
                        data: newData
                    };
                } else {
                    return element;
                }
            });
            this.data.history.forEach(item => {
                if (item.type == 4) {
                    if (e.detail.value.id == 1 && e.detail.value.name == '进口商品') {
                        item.data = []
                        this.setData({
                            isinternal: 1
                        })
                    } else {
                        item.data = []
                        this.setData({
                            isinternal: 1
                        })
                    }
                }
            })
        }
        console.log('newHistory',  this.data.history)
       
        if (e.detail.value.type == 3) {
            this.data.brandList.forEach(item => {
                if (item.brandid == e.detail.value.brandid) {
                    item.checked = false
                }
            })
            console.log('this.data.catList',this.data.petTypeList,e.detail.value.brandid)
            if(e.detail.value.status == 0) {
                this.data.petTypeList.forEach(item => {
                    if (item.id == e.detail.value.id) {
                        item.checked = false
                    }
                })
                this.data.catList.forEach(item => {
                    if (item.id == e.detail.value.id) {
                        item.checked = false
                    }
                })
            }
          
        } else if (e.detail.value.type == 2) {
            this.data.catList.forEach(item => {
                if (item.id == e.detail.value.id) {
                    item.checked = false
                }
            })
        } else if (e.detail.value.type == 1) {
            this.data.petTypeList.forEach(item => {
                if (item.id == e.detail.value.id) {
                    item.checked = false
                }
            })
        }
        this.setData({
            history: this.data.history
        }, () => {
            this.computeScrollViewHeight()
        });
        // 使用map方法取出id并组成字符串
        let pettype = this.data.history[0].data.map(item => {
            if (item.numId) {
                return item.numId;
            } else if (item.id) {
                return item.id;
            }
        }).join(',');

        let brandid = this.data.history[2].data.map(item => item.brandid).join(',');
        let activityregion = this.data.history[3].data.map(item => item.id);
        this.setData({
            brandid
        })
        this.setData({
            subdata: {
                ...this.data.subdata,
                pettype,
                scatid,
                brandid
            },
            kFlag: true
        })
        let scatid = ""
        if (this.data.subdata.brand_list == undefined) {
            this.data.subdata.brand_list = []
            this.data.subdata.bcat_list = []

        }
        scatid = this.data.history[1].data.map(item => item.id).join(',');
        console.log('this.data.subdata.pettype_list',this.data.subdata.pettype_list)
        if(this.data.subdata.pettype_list == undefined) {
            this.data.subdata.pettype_list = []
        }
        if (this.data.subdata.brand_list.length > 0 && this.data.subdata.bcat_list.length == 0 && this.data.subdata.pettype_list.length == 0) {

        } else if (this.data.subdata.bcat_list.length > 0) {
            let scastid = this.data.history[2].data.map(item => item.id).join(',');
            let scatisd = this.data.subdata.bcat_list.map(item => item.id).join(',')
            let arr1 = scastid.split(',');
            let arr2 = scatisd.split(',');
            console.log('arr1', arr1, )
            let commonElements = arr1.filter(item => arr2.includes(item));
            let result = commonElements.join(',');
            scatid = result
            console.log('测试', scastid, scatid, '==', result)
        }
        console.log('pettype2', pettype, scatid, brandid); // 输出结果为 "96,4,9"
        let data = {
            pettype: pettype,
            bcatids: '',
            scatid: scatid,
            brandid: brandid,
            keyword: this.data.keyword,
            minprice: this.data.lowPrice,
            maxprice: this.data.highPrice,
            activity_region: parseInt(activityregion.join('')),
        }
        console.log('this.data.subdata.shipping', this.data.subdata.shipping)
        if (this.data.subdata.shipping == true) {
            this.getReDistributionMall(data);
        } else {
            this.getProductlist(data);
        }


        const app = getApp();
        app.globalData.historyList = {
            ...this.data.subdata,
            ...data,
            historyList: [...this.data.history]
        };
        this.data.goodsList = []
        if (this.data.subdata.shipping == true) {
            this.setData({
                nvaigateState: true,
                currentPage: 1
            })
        } else {
            this.setData({
                nvaigateState: false,
                currentPage: 1
            })
        }
    },
    /**
     * 是否为进口商品
     * @param {*} e 
     */
    onSwitchChange(e) {
        console.log('e')
        this.setData({
            isinternal: e.detail.value ? 2 : 1
        })
    },
    /**
     * 处理最低价输入事件
     */
    onInputLowPrice(e) {
        console.log('输入s', e)
        this.data.lowPrice = e.detail.lowPrice;
        // 处理最低价输入事件
        this.setData({
            'priceData.lowPrice': e.detail.lowPrice
        });
    },
    onInputHighPrice(e) {
        this.data.highPrice = e.detail.highPrice;
        // 处理最高价输入事件
        this.setData({
            'priceData.highPrice': e.detail.highPrice
        });
    },
    /**
     * 重置筛选内容回调函数方法
     */
    handleOnReset(e) {
        this.setData({
            priceData: {
                lowPrice: '',
                highPrice: ''
            },
            lowPrice: '',
            highPrice: '',
            isinternal: ''
        });
        const {
            brandList,
            catList,
            petTypeList
        } = this.data;
        if (e.detail) {
            catList.forEach(element => {
                element.checked = false
            });

            // this.data.history[e.detail].data = []
        }


    },
    /**
     * 跳转商品详情页回调函数方法
     */
    handleGoodsDetail(e) {
        const {
            value
        } = e.currentTarget.dataset
        let params = {}
        // if (this.data.subdata.shipping == true) {
        //     params =   {
        //         goodsId: "",
        //         area: "",
        //         detailType: 1,
        //         act: " oneGoodsInfo",
        //         showMians: this.data.showMians,

        //     }
        // } else {

        // }
        params = {
            goodsId: "",
            area: "",
            detailType: 1,
            act: "good_detail",
            showMians: this.data.showMians
        }
        if (value) {
            const {
                goodsid,
                id
            } = e.currentTarget.dataset.value
            if (goodsid) {
                params.goodsId = goodsid
            } else {
                params.goodsId = id
            }
        } else {
            const {
                id
            } = e.detail
            params.goodsId = id

        }

        const app = getApp();
        console.log('详情', app.globalData.historyList, params)
        wx.navigateTo({
            url: `/pages/goods_detail/index?data=${JSON.stringify(params)}`,
        })
    },

    /**
     * 选择商品规格加入购物车回调函数方法
     */
    handleJoinCart(e) {
        console.log('e', e)
        let data = {
            ...e.currentTarget.dataset.value
        }
        if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
            this.setData({
                isAhow: true,
            })
            return
        }
        //详细接口
        getNavigationbar({
            act: 'good_detail',
            id: data.id
        }).then(res => {
            res.show = true
            data = {
                ...res,
                ...e.currentTarget.dataset.value
            }
          
            console.log('详细接口', res, this.data.subdata.shipping)
           
            // this.setData({
            //     isSpecs: true,
            //     objData: data
            // })
            if (this.data.subdata.shipping == true) {

                this.setData({
                    pecs: true,
                    isSpecs: true,
                    objData: data
                })
            } else {
                data.real_price = data.property && data.property[0].child[0] ? data.property[0].child[0].price : data.real_price
                data.real_price = parseFloat(data.real_price).toFixed(2)
                data.tips = 2
                this.setData({
                    pecs: false,
                    isSpecs: true,
                    objData: data
                })
            }
        })

    },
    /**
     * 完成规格选择将商品添加至购物车
     */
    handleConfirm(e) {
        console.log('传回', e)
        let data = e.detail
        this.setData({
            isSpecs: false
        })
        if (data.type == 0 || data.tips == 0) { //普通
            getNavigationbar({
                act: 'cart',
                id: data.commodityId || '',
                propertyid: data.data.propertyid || '',
                numbers: data.count || '',

            }).then(res => {
                console.log('添加编辑购物车-一件代发', res)
                if (res.result == 101) {
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: res.msg
                        })
                    })
                } else {
                    this.getReMallShopping()
                
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: "商品已添加至购物车"
                        })
                    })

                }
            });
        } else if (data.type == 1 || data.tips == 1) { //一件代发
            ReDistributionMall({
                act: 'addEditMallCart',
                productid: data.commodityId || '',
                propertyid: data.data.propertyid || '',
                numbers: data.count || '',

            }).then(res => {
                console.log('添加编辑购物车-商品列表', res)
                if (res.result == 101) {
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: res.msg
                        })
                    })
                } else {
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: "商品已添加至购物车"
                        })
                    })
                   
                    this.getShoppingCartList()
                    // wx.navigateTo({
                    //     url: `/pages/behalf-shopping-cart/index?data=${JSON.stringify(res)}`,
                    // })
                }

            });
        }

    },
    getReMallShopping(item) {
        this.data.numGood = 0
        getNavigationbar({
            act: 'getcartsp',

        }).then(res => {
            console.log('购物车列表-数量----------', res)
            this.setData({
                numGood: res.numbers
            })

        });
    },
    getShoppingCartList(item) {
        this.data.numGood = 0
        ReDistributionMall({
            act: 'getMallCartsp',

        }).then(res => {
            console.log('购物车列表-一件代发', res)
            this.setData({
                numGood: res.numbers
            })

        });
    },

    nood() {
        console.log('点击', this.data.subdata)
        if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
            this.setData({
                isAhow: true,
            })
            return
        }
        console.log('测试', this.data.subdata.shipping)
        if (this.data.subdata.shipping == true) {
            wx.navigateTo({
                url: `/pages/behalf-shopping-cart/index`,
            })
        } else {
            wx.reLaunch({
                url: `/pages/goods_cart/index`,
            })
        }

    },
    /**
     * 商品列表
     */
    getProductlist(item) {
        // wx.showLoading({
        //     title: '加载中...',
        //     mask: true,
        // });
        this.data.showOUt = false
        console.log('item.brandid', item)
        this.setData({
            item: item,
            orderFlag: true
        })
        this.data.showMians = false
        getNavigationbar({
            act: 'pro_list',
            agentid: this.data.subdata.agentid || '',
            activity_region: item.activity_region || '',
            price_block: item.price_block || '',
            pettype: item.pettype || '',
            bcatid: '',
            scatid: item.scatid || '',
            isinternal: this.data.isinternal || '',
            brandid: item.brandid || this.data.brandid || '',
            keyword: this.data.keyword || '',
            source: '',
            lack_inventory: '1',
            p: this.data.currentPage, // 当前页码
        }).then(res => {
            console.log('普通商品列表', res)
            res.forEach(element => {
                setTimeout(() => {
                    this.data.showOUt = true
                    wx.hideLoading();
                    if (item.pettype == undefined && item.scatid == undefined && item.brandid == undefined) {
                        Object.assign(element, {
                            tips: 0 //普通
                        })
                    } else {
                        Object.assign(element, {
                            tips: 2 //京东
                        })
                    }

                    if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
                        element.sellprice = '***'
                        element.marketprice = '***'
                        // element.replace_price = '***'


                    } else {
                        element.marketprice = parseFloat(element.marketprice).toFixed(2)
                    }
                }, 1000)
                // console.log('elementitem', item)


            });

            wx.nextTick(() => {
                let arr = res

                arr = arr.map(obj => {
                    const uniqueIds = [...new Set(obj.activity_list?.map(item => item.range))];
                    const filteredActive = uniqueIds.map(range => obj.activity_list.filter(item => item.range === range).pop());
                    return {
                        ...obj,
                        activity: filteredActive.reverse()
                    };
                });
                console.log('分类数据列表', arr)
                // 更新页面数据
                this.setData({
                    goodsList: this.data.goodsList.concat(arr), // 将新数据追加到原有数据后面
                    orderListMian: false,
                    loading: false,

                })
            })
        }).catch(() => {
            this.setData({
                loading: false
            })
        })
    },

    /**
     * 计算页面可滚动区域的动态高度
     */
    computeScrollViewHeight() {
        let that = this
        let query = wx.createSelectorQuery().in(this)
        query.select('.content').boundingClientRect()
        query.exec(res => {
            // 同步获取系统信息
            const systemInfo = wx.getSystemInfoSync();
            // 计算底部安全距离
            const safeBottom = systemInfo.screenHeight - systemInfo.safeArea.bottom;
            let contentHeight = res[0].height;
            that.setData({
                scrollViewHeight: contentHeight - safeBottom
            })
        })
    },
    handleScrollToLower() {
        // if (this.data.showOUt == false) {
        //       return
        // }
        let pettype = ''
        let scatid = ''
        let brandid = ''
        if (this.data.history && this.data.history.length) {
            // 使用map方法取出id并组成字符串
            pettype = this.data.history[0].data.map(item => {
                if (item.numId) {
                    return item.numId;
                } else if (item.id) {
                    return item.id;
                }
            }).join(',');
            scatid = this.data.history[1].data.map(item => item.id).join(',');
            brandid = this.data.history[2].data.map(item => item.brandid).join(',');
        }
        let combinedString = ''
        if (this.data.lowPrice != '' && this.data.highPrice != '') {
            combinedString = this.data.lowPrice + ',' + this.data.highPrice;
        } else {
            combinedString = ''
        }
        let activityregion = ''
        console.log('this.data.history', this.data.history)
        if (this.data.history.length != 0) {
            activityregion = this.data.history[3].data.map(item => item.id);
        }

        console.log('pettype2', pettype, scatid, brandid); // 输出结果为 "96,4,9"
        let data = {
            pettype: pettype,
            bcatids: '',
            scatid: scatid,
            brandid: brandid,
            keyword: this.data.keyword,
            minprice: this.data.lowPrice,
            maxprice: this.data.highPrice,
            currentPage: this.data.currentPage + 1,
            price_block: combinedString,
            counts: 10,
            activity_region: activityregion != '' ? parseInt(activityregion.join('')) : '',
        }
        this.setData({
            currentPage: this.data.currentPage + 1,
            loading: true
        });
        if (this.data.subdata.shipping == true) {
            this.getReDistributionMall(data);
        } else {
           
            this.getProductlist(data);
        }

    },
    /**
     * 分类
     */
    getBrandcatlist() {
        const obj = {}
        if (this.data.subdata.shipping == true) {
            obj.type = 1
        }
        getNavigationbar({
            act: 'brand_cat_list',
            ...obj
        }).then(res => {
            console.log('this.data.history=》》》》》',this.data.history,res)
            res.brand_list.forEach(item => {
                item.checked = false;
                item.type = 3;
                this.data.history.forEach(history => {
                    if (history.type == 3) {
                        history.data.forEach(data => {
                            if (data.brandid == item.brandid) {
                                item.checked = true;
                            }
                        })
                    }
                })
            })
            res.cat_list.forEach(item => {
                item.checked = false;
                item.type = 2;
                this.data.history.forEach(historys => {
                    if (historys.type == 2) {
                        historys.data.forEach(data => {
                            
                            if (data.id == item.id) {
                               
                                item.checked = true;
                            }
                        })
                    } else {
                        historys.data.forEach(data => {
                            
                            if (data.id == item.id) {
                               
                                item.checked = true;
                            }
                        })
                    }
                })
            })
            console.log('eeeee', res.cat_list)
            res.pettype_list.forEach(item => {
                item.checked = false;
                item.type = 1;
                this.data.history.forEach(history => {
                    if (history.type == 1) {
                        history.data.forEach(data => {
                            if (data.id == item.id) {
                                item.checked = true;
                            }
                        })
                    }else {
                        history.data.forEach(data => {
                            if (data.id == item.id) {
                                item.checked = true;
                            }
                        })
                    }
                })
            })
            this.setData({
                history: this.data.history,
                columns: res.brand_list,
                brandList: res.brand_list,
                catList: res.cat_list,
                petTypeList: res.pettype_list
            });
            this.setData({
                columns: res.cat_list,
            });
            this.setData({
                columns: res.pettype_list,
            });
            

        });
    },
    /**
     * 一件代发列表
     */
    getReDistributionMall(item) {
        console.log('iten', item)
        this.data.showMians = true
        ReDistributionMall({
            act: 'oneGoodsLists',
            minprice: item.minprice || '',
            maxprice: item.maxprice || '',
            pettype: item.pettype || '',
            bcatid: '', //大类id
            scatid: item.scatid || '', // 小类id
            isinternal: this.data.isinternal || '', // 是否进口 1国内的 2进口的
            brandid: item.brandid || '', // 品牌ID
            keyword: item.keyword || '',
            lack_inventory: '1',
            p: this.data.currentPage, // 当前页码

        }).then(res => {
            // 处理返回的数据
            res.forEach(element => {
                Object.assign(element, {
                    tips: 1 // 一件代发
                })

                if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
                    // element.sellprice = '***'
                    element.replace_price = '***'
                    element.marketprice = '***'
                }
                element.marketprice = parseFloat(element.marketprice).toFixed(1)
            });
            console.log('一件代发', res)
            wx.nextTick(() => {
                // 更新页面数据
                this.setData({
                    //   goodsList: this.data.goodsList.concat(res) // 将新数据追加到原有数据后面
                    goodsList: this.data.goodsList.concat(res), // 将新数据追加到原有数据后面

                    // orderListMian:true,
                }, () => {
                    this.computeScrollViewHeight()
                })
            })
        });
    },
    onClickLeft() {
        console.log('this.data.navigateToUrl', this.data.navigateToUrl)
        wx.switchTab({
            url: `/pages/${this.data.navigateToUrl}/index`
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let objStr = decodeURIComponent(options.obj);

        let obj = JSON.parse(objStr);
        this.setData({
            navigateToUrl: obj.url,

        })

        let res = wx.getStorageSync('USER')
        this.data.obj = res
        console.log('来', obj)
        this.data.flag = obj.flag
        let historyList = [];



        if (obj.keyword && obj.flag == 0) {
            // 处理有关键字的情况
            this.setData({
                keyword: obj.keyword
            })
        } else if (obj.keyword && obj.flag == 3) {
            // 处理有关键字的情况
            this.setData({
                keyword: obj.keyword
            })
        } else if (obj.flag == 1) {
            this.data.keyword = obj.keyword;
            this.setData({
                keyword: obj.keyword
            })
            console.log(obj.historyList.historyList)

            historyList = obj.historyList.historyList
            obj.historyList.keyword = obj.keyword
            obj = {
                ...obj.historyList,
                ...obj
            }
        } else if (obj.isCart) {

        } else {
            console.log('55555', obj)

            let type1Data = obj.classAname ? [{
                ...obj.classAname
            }] : [];
            let type2Data = obj.classBname ? [{
                ...obj.classBname
            }] : [];
            let type3Data = [];
            if (obj.shipping || obj.keyword == '') {
                type3Data = [];
            } else {

                if (obj.brand_list != undefined) {
                    obj.brand_list.forEach(element => {
                        Object.assign(element, {
                            type: 3,
                            brandid: element.id
                        })
                        type3Data.push(element)
                    });

                    if (obj.type != 'Issuing') {
                        if (obj.brand_list.length > 0 && obj.bcat_list.length == 0 && obj.pettype_list.length == 0) {
                            obj.brandid = obj.brand_list.map(item => item.id).join(',')
                        } else if (obj.bcat_list.length > 0) {
                            obj.scatid = obj.bcat_list.map(item => item.id).join(',')
                        }
                    } else {
                        if (obj.brand_list.length > 0) {
                            obj.brandid = obj.brand_list.map(item => item.id).join(',')
                        }
                    }


                } else {
                    type3Data = [{
                        ...obj
                    }];
                }

            }
            historyList.push({
                type: 1,
                data: type1Data
            }, {
                type: 2,
                data: type2Data
            }, {
                type: 3,
                data: type3Data
            }, {
                type: 4,
                data: []
            });
        }
        if (obj.flag == 1) {
            this.data.subdata = {
                ...obj.historyList
            }
        } else {
            this.data.subdata = {
                ...obj
            }
        }

        console.log('historyList', historyList)
        const app = getApp();
        app.globalData.historyList = {
            ...this.data.subdata,
            ...obj,
            ...obj.shipping,
            historyList: [...historyList, ...this.data.history]
        };
        wx.nextTick(() => {
            this.setData({
                history: [...historyList],
                subdata: this.data.subdata
            }, () => {
                this.computeScrollViewHeight()
            });
        });




        this.getBrandcatlist();

        if (this.data.subdata.shipping == true) {
            this.setData({
                nvaigateName: '一件代发专区'
            })
            // this.data.subdata.value.keyword = this.data.subdata.keyword
            this.getReDistributionMall({
                ...this.data.subdata
            });
            
            this.getHttpSwiperList()
             this.getShoppingCartList();
        } else {
            this.data.goodsList = []
            this.getProductlist({
                ...this.data.subdata,
            });this.getReMallShopping()
           
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      
        if (this.data.subdata.shipping == true) {
       
             this.getShoppingCartList();
        } else {
          this.getReMallShopping()
           
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
 

})